#include "Editor.h"
#include "GameScene.h"
#include "GameManager.h"
//#include "Gallery.h"
#include "cocos-ext.h"
USING_NS_CC;
USING_NS_CC_EXT;

static CCPoint ques[4] = { ccp(40,280), ccp(120,280), ccp(200,280),ccp(280,280)};
static CCPoint ans[4] = {ccp(40,200), ccp(120,200), ccp(200,200),ccp(280,200) };
static CCPoint opts[8] = { ccp(40,40), ccp(120,40), ccp(200,40),ccp(280,120),
											ccp(40,120),  ccp(120,120), ccp(200,120),ccp(280,120)};

CCScene* Editor::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	Editor *layer = Editor::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

bool Editor::init()
{
	if ( !CCLayer::init() )
	{
		return false;
	}

	/*std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("maze2.xml");  
	TiXmlDocument* xmlDoc = new TiXmlDocument(fullPath);  
	xmlDoc->LoadFile();  
	TiXmlElement* rootElement = xmlDoc->RootElement();  
	TiXmlElement* levelElement = rootElement->FirstChildElement();  
	while(levelElement)
	{
		MazeInfo *mi = new MazeInfo;
		mi->level = atoi(levelElement->Attribute("id"));
		nextLevel = mi->level + 1;
		mi->rows = atoi(levelElement->Attribute("rows"));
		mi->columns = atoi(levelElement->Attribute("columns"));
		std::string ques = levelElement->Attribute("questions");
		std::vector<std::string> qv = stringsplit(ques, '|');
		for (int i=0; i< qv.size(); i++)
		{
			mi->questions.push_back(qv[i]);
		}

		TiXmlElement *stepEle = levelElement->FirstChildElement();
		int cstep = 0;

		while (stepEle)
		{
			mi->steps[cstep] = new StepInfo;
			std::string answs = stepEle->Attribute("answers");
			std::string options = stepEle->Attribute("options");

			std::vector<std::string> av = stringsplit(answs, '|');
			for (int i=0; i< av.size(); i++)
			{
				mi->steps[cstep]->answers.push_back(atoi(av[i].c_str()));
			}
			std::vector<std::string> ov = stringsplit(options, '|');
			for (int i=0; i< ov.size(); i++)
			{
				mi->steps[cstep]->options.push_back(ov[i]);
			}
			g_maze[mi->level] = mi;

			stepEle = stepEle->NextSiblingElement();
			++cstep;
		}
		levelElement = levelElement->NextSiblingElement();
	}

	CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);

	for (int i=0; i<4; ++i)
	{
		CCSprite *sp = CCSprite::create("border2.png");
		sp->setPosition(ques[i]);
		addChild(sp);
	}
	for (int i=0; i<4; ++i)
	{
		CCSprite *sp = CCSprite::create("border2.png");
		sp->setPosition(ans[i]);
		addChild(sp);
	}
	for (int i=0; i<8; ++i)
	{
		CCSprite *sp = CCSprite::create("border2.png");
		sp->setPosition(opts[i]);
		addChild(sp);
	}
		
	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		this,
		menu_selector(Editor::save));
	pCloseItem->setTag(1);

	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
		origin.y + pCloseItem->getContentSize().height/2));

	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);

	CCMenuItemFont::setFontName("Marker Felt");
	CCMenuItemFont::setFontSize(22);
	CCMenuItemFont* pFontMenuItem1 = CCMenuItemFont::create("last", this, menu_selector(Editor::loadLastLevel));
	CCMenu *m = CCMenu::create(pFontMenuItem1, NULL);
	m->setPosition(ccp(visibleSize.width/2 - 50, visibleSize.height - 30));
	addChild(m);
	CCMenuItemFont* pFontMenuItem2 = CCMenuItemFont::create("next", this, menu_selector(Editor::loadNextLevel));
	CCMenu *m2 = CCMenu::create(pFontMenuItem2, NULL);
	m2->setPosition(ccp(visibleSize.width/2 + 50, visibleSize.height - 30));
	addChild(m2);

	CCMenuItemFont* pFontMenuItem3 = CCMenuItemFont::create("step<", this, menu_selector(Editor::loadLastStep));
	CCMenu *m3 = CCMenu::create(pFontMenuItem3, NULL);
	m3->setPosition(ccp(visibleSize.width/2 - 150, visibleSize.height - 30));
	addChild(m3);
	CCMenuItemFont* pFontMenuItem4 = CCMenuItemFont::create("step>", this, menu_selector(Editor::loadNextStep));
	CCMenu *m4 = CCMenu::create(pFontMenuItem4, NULL);
	m4->setPosition(ccp(visibleSize.width/2 + 150, visibleSize.height - 30));
	addChild(m4);

	CCScale9Sprite* sacel9SprY = CCScale9Sprite::create("border.png");
	CCEditBox* box = CCEditBox::create(CCSizeMake(80, 40), sacel9SprY); 
	box->setPosition(ccp(visibleSize.width/2, visibleSize.height - 30));
	box->setMaxLength(3);
	box->setInputMode(kEditBoxInputModeAny);
	//box->setDelegate(this);
	box->setPlaceHolder("level");
	addChild(box);

	curmi = new MazeInfo;
	cursi = new StepInfo;
	curmi->level = nextLevel;
	g_maze[nextLevel] = curmi;
	seleceMode = -1;
	quesArr = CCArray::createWithCapacity(4);
	quesArr->retain();
	ansArr = CCArray::createWithCapacity(4);
	ansArr->retain();
	optArr = CCArray::createWithCapacity(4);
	optArr->retain();
	selectedImage = CCArray::createWithCapacity(8);
	selectedImage->retain();
	curLevel = 0;
	curStep = 0;*/
	return true;
}

bool Editor::check()
{
	/*if(curmi->columns ==0 || curmi->rows ==0) {
		showPoplayer("current maze no data");
		return false;
	}*/
	return true;
}

void Editor::save(CCObject* pSender)
{
	/*if(!check())
		return;

	g_maze[nextLevel] = curmi;
	curmi->rows = curmi->steps.size() + 1;
	curmi->columns = curmi->questions.size();
	curmi->steps[curStep] = cursi;

	std::string fullPath = CCFileUtils::sharedFileUtils()->fullPathForFilename("maze2.xml");  
	TiXmlDocument* xmlDoc = new TiXmlDocument(fullPath);  
	xmlDoc->LoadFile();  
	TiXmlElement* rootElement = xmlDoc->RootElement();  
	for (int i=1; i<=nextLevel; ++i)
	{
		MazeInfo *mi = g_maze[i];

		 TiXmlElement *levelElement = new TiXmlElement("Level");
		 levelElement->SetAttribute("id", i);
		 levelElement->SetAttribute("rows", mi->rows);
		 levelElement->SetAttribute("columns", mi->columns);
		 std::stringstream ss;
		 for (int j=0; j<mi->questions.size(); ++j)
		 {
			 if(j != mi->questions.size() - 1 )
				ss<<mi->questions[j]<<"|";
			 else
				ss<<mi->questions[j];
		 }
		 levelElement->SetAttribute("questions", ss.str().c_str());

		 for (int j=0; j<mi->steps.size(); ++j)
		 {
			 StepInfo *si = mi->steps[j];
			 TiXmlElement *stepElement = new TiXmlElement("Step");

			 std::stringstream ans;
			 for (int k=0; k<si->answers.size(); ++k)
			 {
				 if(k != si->answers.size() - 1)
					ans<<si->answers[k]<<"|";
				 else
					 ans<<si->answers[k];
			 }
			 std::stringstream opt;
			 for (int k=0; k<si->options.size(); ++k)
			 {
				 if(k != si->options.size() - 1)
					 opt<<si->options[k]<<"|";
				 else
					 opt<<si->options[k];
			 }

			 stepElement->SetAttribute("options", opt.str().c_str());
			
			 levelElement->LinkEndChild(stepElement);
		 }
		 rootElement->LinkEndChild(levelElement);
	}
	xmlDoc->SaveFile();*/
}

bool Editor::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
	/*CCPoint touchPoint = touch->getLocationInView();  
	touchPoint = CCDirector::sharedDirector()->convertToGL(touchPoint);

	if(touchPoint.y < 160)
		seleceMode = 2;
	else if(touchPoint.y >=160 && touchPoint.y <= 240)
		seleceMode = 1;
	else if(touchPoint.y > 240 && touchPoint.y < 300)
		seleceMode = 0;

	if (touchPoint.y < 300)
	{
		selectedPoint = touchPoint;
		CCScene *scene = CCScene::create();
		Gallery *layer = new Gallery;
		layer->init();
		layer->ed = this;
		scene->addChild(layer);
		layer->autorelease();
		CCDirector::sharedDirector()->pushScene(scene);
		return true;
	}
	*/
	return true;
}

void Editor::ccTouchMoved(CCTouch* touch, CCEvent* event)
{

}

void Editor::ccTouchEnded(CCTouch* touch, CCEvent* event)
{

}

void Editor::loadLastStep(CCObject* pSender)
{
	/*if(curStep ==0)
		return;
	displayAnsOps(g_editLevel, curStep-1);*/
}

void Editor::loadNextStep(CCObject* pSender)
{
	/*CCObject *sp;
	CCARRAY_FOREACH(ansArr, sp)
	{
		CCSprite *target = dynamic_cast<CCSprite*>(sp);
		target->removeFromParent();
	}
	ansArr->removeAllObjects();

	CCARRAY_FOREACH(optArr, sp)
	{
		CCSprite *target = dynamic_cast<CCSprite*>(sp);
		target->removeFromParent();
	}
	optArr->removeAllObjects();

	cursi->answers.clear();
	cursi->options.clear();

	displayAnsOps(curLevel, curStep+1);*/
}

void Editor::loadLastLevel(CCObject* pSender)
{

}
void Editor::loadNextLevel(CCObject* pSender)
{

}

void Editor::loadLevel(int l)
{
	/*MazeInfo *mi  = g_maze[l];
	if(!mi)
		return;
	for (int i=0; i<mi->questions.size(); ++i)
	{
		string s = mi->questions[i];
		CCSprite *sp = CCSprite::createWithSpriteFrameName( s.c_str() );
		sp->setPosition(ques[i]);
		addChild(sp);
		quesArr->addObject(sp);
	}
	displayAnsOps(l, 0);*/
}

void Editor::displayAnsOps(int l, int s)
{
	/*MazeInfo *mi  = g_maze[l];
	if(s >= mi->steps.size())
		return;
	int i = 0;
	StepInfo *si = mi->steps[i];
	for (int j=0; j< si->options.size(); ++j)
	{
		string ss =  si->options[j];
		CCSprite *sp = CCSprite::createWithSpriteFrameName( ss.c_str() );
		sp->setPosition(opts[j]);
		addChild(sp);
		optArr->addObject(sp);
	}
	for (int j=0; j< si->answers.size(); ++j)
	{
		int k = si->answers[j];
		string ss =  si->options[k];
		CCSprite *sp = CCSprite::createWithSpriteFrameName( ss.c_str() );
		sp->setPosition(ans[j]);
		addChild(sp);
		ansArr->addObject(sp);
	}*/
}

void Editor::onEnter()
{
	/*CCLayer::onEnter();
	if(selectedImage->count() > 0)
	{
		CCObject *obj;
		CCARRAY_FOREACH(selectedImage, obj)
		{
			CCString *cs = (CCString *)obj;
			CCSprite *sp = CCSprite::createWithSpriteFrameName(cs->m_sString.c_str());
			if(seleceMode == 0) {
				int idx = quesArr->count();
				sp->setPosition(ques[idx]);
				quesArr->addObject(sp);
				curmi->questions.push_back(cs->m_sString);
			}
			else if(seleceMode == 1) {
				int idx = ansArr->count();
				sp->setPosition(ans[idx]);
				ansArr->addObject(sp);
				cursi->answers.push_back(0); 
			} 
			else if (seleceMode == 2)
			{
				int idx = optArr->count();
				sp->setPosition(opts[idx]);
				optArr->addObject(sp);
				cursi->options.push_back(cs->m_sString);
			}
			addChild(sp);
		}
		
		selectedImage->removeAllObjects();
	}*/
}

